<?php
/**
 * 资讯模型
 * 
 * @version $Id: Topic.php 664 2013-01-24 07:58:44Z linsir123 $
 */
class modelTopic 
{
	/**
	 * @var API地址
	 */
	public $url = "http://news.baidu.com/ns?";
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		///
	}
	
	/**
	 * 
	 */
	public function getList($keyword, $max = 30)
	{
		return $this->_spider($keyword, $max);
	}
	
	
	/// 以下为私有方法 /// 
	
	
	/**
	 * 取得某个分类对应的关键字列表
	 * // 抓取`淘宝排行榜`
	 */
	private function _spider($keyword, $max)
	{
		$params = array(
			'word' => iconv("UTF-8", "GBK", $keyword), 
			'rn' => $max, 
			'tn' => "newsrss", 
			//'from' => "news", 
			//'cl' => 2, 
			//'ct' => 0, 
		);
		$url = $this->url;
		foreach ($params as $k => $v) 
			$url .= "$k=" . urlencode($v) . "&"; 
			
		///
		$list = array();
		$content = lpApi::curl($url);
		if ( ! empty($content)) {
			$content = iconv("GBK", "UTF-8", $content);
			$content = str_replace("gb2312", "utf-8", $content);
			
			///
			$xml = simplexml_load_string($content);
			if (isset($xml->channel->item)) 
				foreach ($xml->channel->item as $v) {
					$list[] = array(
						'title' => (string) $v->title,
						'link' => (string) $v->link,
					);
			}
		}
		
		///
		return $list;
	}
}
